package cn.edu.ecut.annotation;

import java.lang.reflect.Field;

public class Test {

	public static void main(String[] args) throws Exception {
		
		Object o = new Panda();
		System.out.println( o );
		
		Class<?> c = o.getClass();
		
		final Field idField = c.getDeclaredField( "id" ); // NoSuchFieldException
		Assign idAssign = idField.getAnnotation( Assign.class ); // 获得指定类型的注解
		System.out.println( idAssign );
		System.out.println( System.identityHashCode( idAssign ) );
		final String idValue = idAssign.value(); // 从idAssign中获取value属性取值时又TM是调用方法的形式
		System.out.println( idValue );
		
		idField.setAccessible( true );
		idField.set( o , Integer.parseInt( idValue ) ); // o.id = Integer.parseInt( idValue ) ;
		
		final Field nameField = c.getDeclaredField( "name" );
		Assign nameAssign = nameField.getAnnotation( Assign.class );
		System.out.println( nameAssign );
		System.out.println( System.identityHashCode( nameAssign ) );
		final String nameValue = nameAssign.value(); // 从nameAssign中获取value属性取值时又TM是调用方法的形式
		System.out.println( nameValue );
		
		nameField.setAccessible( true );
		// IllegalAccessException
		nameField.set( o ,  nameValue ); // o.name = nameValue ;
		
		System.out.println( o );
	}

}
